﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;

namespace QA_System
{
    public class WordStruct
    {
        private string word;

        private string wordId;

        private string pos;

        private string wordRoleinSentences;

        private string wordSubmean;

        private int ower;

        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }

        public int Ower
        {
            set { ower = value; }

            get { return ower; }
        }

        public string Word
        {
            set { word = value; }
            get { return word; }
        }

        public string WordID
        {
            set { wordId = value; }
            get { return wordId; }
        }

        public string Pos
        {
            set { pos = value; }
            get { return pos; }
        }

        public string WordRoleInSentences
        {
            set { wordRoleinSentences = value; }
            get { return wordRoleinSentences; }
        }

        public string WordSubMean
        {
            set { wordSubmean = value; }
            get { return wordSubmean; }
        }

        public WordStruct()
        {
            word = "";
            wordId = "";
            pos = "";
            wordRoleinSentences = "";
            wordSubmean = "";
            ower = -2;
            index = 0;
        }


        public WordStruct(string InWord, string InWordId, string InWordRole, string InWordRoleSentences, string InWordSubmean, int HumanOwer)
        {
            word = InWord;
            wordId = InWordId;
            pos = InWordRole;
            wordRoleinSentences = InWordRoleSentences;
            wordSubmean = InWordSubmean;
            ower = HumanOwer;
            index = 0;
        }

        

        
    }

    public class SentenceStruct
    {
        private WordStruct[] wordarray;

        private int sentenceLenghth;

        private string strSentencePos;

        public string StrSentencePos
        {
            get
            {
                strSentencePos = "";
                for (int i = 0; i < this.sentenceLenghth; i++) strSentencePos += this[i].Pos + " ";
                return strSentencePos;
            }
        }

        public WordStruct this[int i]
        {
            set { wordarray[i] = value; }

            get { return wordarray[i]; }
        }

        public int SentenceLength
        {
            set { sentenceLenghth = value; }

            get { return sentenceLenghth; }
        }

        public SentenceStruct(sentence_tagger sentag)
        {
            wordarray = PrepareStructure.WordArrayFormArray(sentag);

            strSentencePos = "";
            sentenceLenghth = wordarray.Length;

        }


        private int GetNextNounPosition(int index)
        {
            try
            {
                int result = -1;
                int i = 0;
                for (i=index;i< SentenceLength; i++)
                {
                    if ((this[i].Pos.Contains("N")) || (this[i].Pos.Contains("P")))
                    {
                        result = this[i].Index;
                        break;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // return index of both N and Np
        public int[] GetNounsPosition()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth-1; i++)
                {
                    string strSpecial = "bạn,tôi,tớ,mình,cậu,chú,ông,tao,mày,anh";
                    if ((wordarray[i].Pos.Contains("N"))&&(!wordarray[i+1].Pos.Equals("Np")))
                    {
                        if (!strSpecial.Contains(wordarray[i].Word.ToLower()))
                        {
                            position[j] = i;
                            j++;
                        }
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        // return index of Np
        public int[] GetNpPosition()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth; i++)
                {
                    if ((wordarray[i].Pos.Equals("Np")))
                    {
                        position[j] = i;
                        j++;
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int[] GetHumanNounsPosition()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth - 1; i++)
                {
                    string strSpecial = "bạn,tôi,tớ,mình,cậu,chú,ông,tao,mày,anh";
                    if ((wordarray[i].Pos.Contains("Np")) && (!wordarray[i + 1].Pos.Equals("Np")))
                    {
                        if (!strSpecial.Contains(wordarray[i].Word.ToLower())&&(wordarray[i].WordSubMean.Contains("People")))
                        {
                            position[j] = i;
                            j++;
                        }
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int[] GetHumanNounsPositionForNNElipsis()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth - 1; i++)
                {
                    string strSpecial = "bạn,tôi,tớ,mình,cậu,chú,ông,tao,mày,anh";
                    if ((wordarray[i].Pos.Contains("Np")))
                    {
                        if (!strSpecial.Contains(wordarray[i].Word.ToLower()) && (wordarray[i].WordSubMean.Contains("People")))
                        {
                            position[j] = i;
                            j++;
                        }
                    }
                    else if((wordarray[i + 1].Pos.Equals("P")) && (!wordarray[i+1].WordSubMean.Contains("People")))
                    {
                        position[j]=i;
                        j++;
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int[] GetProNounsPosition()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth; i++)
                {
                    if ((wordarray[i].Pos.Contains("P")) && (!wordarray[i].Word.Equals("nào")) && (!wordarray[i].Word.Equals("gì"))&&(!wordarray[i].Word.ToLower().Equals("tôi")))
                    {
                        position[j] = i;
                        j++;
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int[] GetAskWorkPosition()
        {
            try
            {
                int[] position = new int[this.SentenceLength];
                int i=0;
                for (int j=0; j<this.SentenceLength;j++)
                {
                    if (this[j].Pos.Equals("Z") || this[j].Word.ToLower()=="làm gì")
                    {
                        position[i] = j;
                        i++;
                    }
                }
                int[] finalArray = new int[i];
                for (int j = 0; j < i; j++)
                {
                    finalArray[j] = position[j];
                }
                return finalArray;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public WordStruct GetTObjectofVerb(WordStruct verb)
        {
            try
            {
                WordStruct wsObject = null;
                int position = this.GetNextNounPosition(verb.Index);
                if (position > -1)
                {
                    if (this[position].WordSubMean.Contains("Things"))
                    {
                        wsObject = this[position];
                    }
                    else
                    {
                        position = this.GetNextNounPosition(position);
                        if (position > -1)
                        {
                            if (this[position].WordSubMean.Contains("Things"))
                            {
                                wsObject = this[position];
                            }
                        }
                    }
                }
                return wsObject;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public WordStruct GetHOjectofVerb(WordStruct verb)
        {

            try
            {
                WordStruct wsObject = null;
                int position = this.GetNextNounPosition(verb.Index);
                if (position > -1)
                {
                    if (this[position].WordSubMean.Contains("People"))
                    {
                        wsObject = this[position];
                    }
                    else
                    {
                        position = this.GetNextNounPosition(position);
                        if (position > -1)
                        {
                            if (this[position].WordSubMean.Contains("People"))
                            {
                                wsObject = this[position];
                            }
                        }
                    }
                }
                return wsObject;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public WordStruct GetSubjectofVerb(WordStruct verb)
        {
            try
            {
                WordStruct wsNoun = null;
                if (verb.Index > 0)
                {
                     if (this[verb.Index - 1].Pos.Equals("R"))
                     {
                         if (verb.Index > 1)
                         {
                             if (this[verb.Index - 2].Pos.Contains("N"))
                             {
                                 wsNoun = this[verb.Index - 2];
                             }
                             else
                             {
                                 if (this[verb.Index - 2].Pos.Contains("P"))
                                 {
                                     wsNoun = this[this[verb.Index - 2].Ower];
                                 }
                                 else
                                 {
                                     if (this.GetLastVerbPosition(0, verb.Index -1) != -1)
                                     {
                                         WordStruct preverb = this[this.GetLastVerbPosition(0, verb.Index - 1)];
                                         wsNoun = this.GetSubjectofVerb(preverb);
                                     }
                                     else
                                     {
                                         if (this.GetNounsPosition().Length > 0) wsNoun = this[this.GetNounsPosition()[0]];
                                     }
                                 }
                             }
                         }
                     }
                     else
                     {
                         if (this[verb.Index - 1].Pos.Contains("N"))
                         {
                             wsNoun = this[verb.Index - 1]; 
                         }
                         else
                         {
                             if (this[verb.Index - 1].Pos.Contains("P"))
                             {
                                 if(this[verb.Index-1].Ower!=-2)
                                 wsNoun = this[this[verb.Index - 1].Ower];
                             }
                             else
                             {
                                 if (this.GetLastVerbPosition(0, verb.Index - 1) != -1)
                                 {
                                     WordStruct preverb = this[this.GetLastVerbPosition(0, verb.Index - 1)];
                                     wsNoun = this.GetSubjectofVerb(preverb);
                                 }
                                 else
                                 {
                                     if (this.GetNounsPosition().Length > 0) wsNoun = this[this.GetNounsPosition()[0]];
                                 }
                             }
                         }
                     }
                }

                return wsNoun;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // public WordStruct GetNounWordfromindex(int nounIndex) { }

        public int GetVerbPosition(int beginwordIndex, int lastwordIndex)
        {
            try
            {
                int verbposition = -1;

                for (int i = beginwordIndex; i < lastwordIndex; i++)
                {
                    if ((wordarray[i].Pos.Contains("V")))
                    {
                        verbposition = i;
                        break;
                    }
                }

                return verbposition;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //Get NP position
        public int GetNpPosition(int beginwordIndex, int lastwordIndex)
        {
            try
            {
                int npposition = -1;

                for (int i = beginwordIndex; i < lastwordIndex; i++)
                {
                  if ((wordarray[i].Pos.Contains("Np")) || (wordarray[i].Pos.Contains("P")) )
                    {
                        npposition = i;
                        break;
                    }
                }

                return npposition;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //Get N position
        public int GetNounsPosition(int beginwordIndex, int lastwordIndex)
        {
            try
            {
                int npposition = -1;

                for (int i = beginwordIndex; i < lastwordIndex; i++)
                {
                    if ((wordarray[i].Pos.Contains("N")) || (wordarray[i].Pos.Contains("Np")))
                    {
                        npposition = i;
                        break;
                    }
                }

                return npposition;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Get N position
        public int GetNounsPosition2(int beginwordIndex, int lastwordIndex)
        {
            try
            {
                int npposition = -1;

                for (int i = beginwordIndex; i < lastwordIndex; i++)
                {
                    if ((wordarray[i].Pos.Contains("N")) || (wordarray[i].Pos.Contains("Np")) || (wordarray[i].Pos.Contains("P")))
                    {
                        npposition = i;
                        break;
                    }
                }

                return npposition;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int GetLastVerbPosition(int beginwordIndex, int lastwordIndex)
        {
            try
            {
                int verbposition = -1;

                for (int i = lastwordIndex; i > beginwordIndex; i--)
                {
                    if ((wordarray[i].Pos.Contains("V")) && (!wordarray[i].Pos.Contains("Ve")))
                    {
                        verbposition = i;
                        break;
                    }
                }

                return verbposition;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int[] GetAllVerbPosition()
        {
            try
            {
                int[] verbindex = new int[25];
                int index = 0;
                for (int j = 0; j < this.SentenceLength; j++)
                {
                    if (this[j].Pos.Contains("V"))
                    {
                        verbindex[index] = this[j].Index;
                        index++;
                    }
                }
                int[] result = new int[index];
                for (int i = 0; i < index; i++) result[i] = verbindex[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int[] GetAllPreVerbPosition()
        {
            try
            {
                int[] preverbindex = new int[5];
                int index = 0;
                for (int j = 0; j < this.SentenceLength; j++)
                {
                    if (this[j].Pos.Contains("R") &&!this[j].Word.ToLower().Equals("rồi"))
                    {
                        preverbindex[index] = this[j].Index;
                        index++;
                    }
                }
                int[] result = new int[index];
                for (int i = 0; i < index; i++) result[i] = preverbindex[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Vinh them: Dung cho VPA: tim vi tri tu giong, the
        public int[] GetAllPreVerbPosition2()
        {
            try
            {
                int[] preverbindex = new int[5];
                int index = 0;
                for (int j = 0; j < this.SentenceLength; j++)
                {
                    if (this[j].Pos.Contains("R") && !this[j].Word.ToLower().Equals("rồi") && (this[j].Word.ToLower().Equals("giống") || this[j].Word.ToLower().Equals("cũng") || this[j].Word.ToLower().Equals("không")))
                    {
                        preverbindex[index] = this[j].Index;
                        index++;
                    }
                }
                int[] result = new int[index];
                for (int i = 0; i < index; i++) result[i] = preverbindex[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int[] GetAllNcPosition()
        {
            try
            {
                int[] position = new int[sentenceLenghth];
                int j = 0;
                for (int i = 0; i < sentenceLenghth; i++)
                {
                    if ((wordarray[i].Pos.Contains("Nc")))
                    {
                        position[j] = i;
                        j++;
                    }
                }
                int[] result = new int[j];
                for (int i = 0; i < j; i++) result[i] = position[i];

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int MeanwordLengh()
        {
            try
            {
                int result = sentenceLenghth +1;
                for (int i = 0; i < sentenceLenghth; i++)
                {
                    if (this[i].Pos.Equals("-")) result--;
                }
                if(result>sentenceLenghth) result = sentenceLenghth;
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int CountTime()
        {
            try
            {
                int result=0;
                for (int i = 0; i < sentenceLenghth; i++)
                {
                    if (this[i].Pos.Equals("M")|| this[i].Pos.Equals("Nu"))
                        result++;
                }
                
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsContain(WordStruct word)
        {
            try
            {
                bool result = false;
                for (int i = 0; i < this.SentenceLength; i++)
                {
                    if (this[i].Word.Equals(word.Word))
                    {
                        result = true;
                        break;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsContain(string word)
        {
            try
            {
                bool result = false;
                for (int i = 0; i < this.SentenceLength; i++)
                {
                    if (this[i].Word.Equals(word))
                    {
                        result = true;
                        break;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    public class PrepareStructure
    {
        static XMLNounConnection nounconn = new XMLNounConnection();

        

        static public sentence_tagger FixSentence(sentence_tagger formerSentence)
        {
            try
            {
                sentence_tagger resultSentence = null;
                sentence_tagger rawSen = new sentence_tagger();
                int i = 0;
                string strPos ="E,P,M"; // nhung tu khien tu "co" la dong tu
                string strWordnomean = "có,là,thế,hay,không";// tu ko co nghia
                rawSen[0].Word_pos = formerSentence[0].Word_pos;
                rawSen[0].Word_text = formerSentence[0].Word_text;
                switch (formerSentence[0].Word_text.ToLower())
                {
                    case "có": if (!strPos.Contains(rawSen[0].Word_pos)) rawSen[0].Word_pos = "-";
                        break;
                    case "là": rawSen[0].Word_pos = "-";
                        break;
                    case "ai": rawSen[0].Word_pos = "Z";
                        break;
                    case "đâu": rawSen[0].Word_pos = "Z";
                        break;
                }
                
                rawSen[0].Word_pos = "";
                rawSen[0].Word_text = "";
                
                i = 1;
                #region gheptu
                for (int index = 0; index < formerSentence.Sentence_length; index++)
                {
                    if (!string.IsNullOrEmpty(formerSentence[index].Word_text))
                    {
                        VPEXMLConnection VPE = new VPEXMLConnection();
                        if (VPE.IsVPE(formerSentence[index].Word_text.ToLower(), "C:\\tagger\\XMLdatabase\\VPEXMLdatabase.xml"))
                        {
                            if (rawSen[i - 1].Word_text.ToLower().Contains("như") || rawSen[i - 1].Word_text.ToLower().Contains("giống"))
                            {
                                rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                rawSen[i - 1].Word_pos = "-";
                            }
                            else
                            {
                                rawSen[i].Word_text = formerSentence[index].Word_text;
                                rawSen[i].Word_pos = "-";
                                i++;
                            }

                        }
                            //Vinh them: ghep tư là dấu hiệu SAnaphora
                        else if ((formerSentence[index].Word_pos == "P") && (rawSen[i-1].Word_text.ToLower().Contains("điều")))
                        {
                            //if (rawSen[i-1].Word_text.ToLower().Contains("điều"))
                            //{
                                rawSen[i-1].Word_text +=" " + formerSentence[index].Word_text;
                                rawSen[i-1].Word_pos ="SA";
                            //}
                        }
                        else if ((formerSentence[index].Word_text == "dài" || formerSentence[index].Word_text=="rộng") && (rawSen[i - 1].Word_text.ToLower().Contains("chiều")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "N";
                        }
                        else if (formerSentence[index].Word_text=="sinh" && (rawSen[i - 1].Word_text.ToLower().Contains("năm") || rawSen[i - 1].Word_text.ToLower().Contains("nơi")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "N";
                        }
                        //Đàn bán nguyệt
                        else if (formerSentence[index].Word_text == "bán nguyệt" && (rawSen[i - 1].Word_text.ToLower().Contains("đàn") ))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Hộp để tro hài cốt
                        else if (formerSentence[index].Word_text == "để" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "tro" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp để")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "hài cốt" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp để tro")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Sách lá buông
                        else if (formerSentence[index].Word_text == "lá" && (rawSen[i - 1].Word_text.ToLower().Contains("sách")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "buông" && (rawSen[i - 1].Word_text.ToLower().Contains("sách lá")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Lọ đựng vôi
                        else if (formerSentence[index].Word_text == "đựng" && (rawSen[i - 1].Word_text.ToLower().Contains("lọ")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "vôi" && (rawSen[i - 1].Word_text.ToLower().Contains("lọ đựng")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Bếp đun
                        else if (formerSentence[index].Word_text == "đun" && (rawSen[i - 1].Word_text.ToLower().Contains("bếp")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Chén trà lễ
                        else if (formerSentence[index].Word_text == "trà" && (rawSen[i - 1].Word_text.ToLower().Contains("chén")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "lễ" && (rawSen[i - 1].Word_text.ToLower().Contains("chén trà")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Ấm nước
                        else if (formerSentence[index].Word_text == "nước" && (rawSen[i - 1].Word_text.ToLower().Contains("ấm")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //Hộp đựng trầu cau
                        else if (formerSentence[index].Word_text == "đựng" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "trầu" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp đựng")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        else if (formerSentence[index].Word_text == "cau" && (rawSen[i - 1].Word_text.ToLower().Contains("hộp đựng trầu")))
                        {
                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                            rawSen[i - 1].Word_pos = "Np";
                        }
                        //else if ((rawSen[i - 1].Word_text.ToLower().Contains("anh ấy")))
                        //{
                        //&&(rawSen[i-1].Word_text.ToLower().Contains("anh ấy"))
                        //    rawSen[i-1].Word_pos = "P";
                        //}
                        else
                        {
                            switch (formerSentence[index].Word_text.ToLower())
                            {
                                case "anh ta":
                                case "chị ta":
                                case "cô ta":
                                case "ông ta":
                                case "anh ấy":
                                    rawSen[i].Word_pos = "P";
                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                    i++;

                                    /* vi i-1 lam mat tu o trc
                                        rawSen[i-1].Word_pos = "P";
                                        rawSen[i-1].Word_text = formerSentence[index].Word_text;
                                        i++;
                                        */
                                    //rawSen[i - 1].Word_pos = "P";

                                    //rawSen[i-1].Word_pos.s
                                    break;
                                case "lồng bàn":
                                    rawSen[i].Word_pos = "Np";
                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                    i++;

                                    /* vi i-1 lam mat tu o trc
                                        rawSen[i-1].Word_pos = "P";
                                        rawSen[i-1].Word_text = formerSentence[index].Word_text;
                                        i++;
                                        */
                                    //rawSen[i - 1].Word_pos = "P";

                                    //rawSen[i-1].Word_pos.s
                                    break;
                                case "này":
                                    if(formerSentence[index].Word_pos=="P")
                                    {
                                        if (rawSen[i - 1].Word_text.ToLower() == "xe" || rawSen[i - 1].Word_text.ToLower() == "cái" || rawSen[i - 1].Word_text.ToLower() == "hiện vật" || rawSen[i - 1].Word_text.ToLower() == "cái hộp" || rawSen[i - 1].Word_text.ToLower() == "cái nón"
                                            || rawSen[i - 1].Word_text.ToLower() == "cái chum")
                                        {
                                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                            rawSen[i - 1].Word_pos = "P";

                                        }
                                            /*
                                        else
                                        {
                                            rawSen[i].Word_text = formerSentence[index].Word_text;
                                            rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                            i++;
                                        }
                                             * */
                                    }
                                    break;
                                    
                                case "xe":
                                    
                                    if (formerSentence[index + 1].Word_text.ToLower() != "trâu" && formerSentence[index + 1].Word_text.ToLower() != "này")
                                    {
                                        rawSen[i].Word_text += formerSentence[index].Word_text + " trâu";
                                        rawSen[i].Word_pos = "Np";
                                        i++;
                                    }
                                        
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                        i++;
                                    }
                                         
                                    
                                    break;
                                    /*
                                case "đàn":

                                    if (formerSentence[index + 1].Word_text.ToLower() == "bán nguyệt")
                                    {
                                        rawSen[i].Word_text += formerSentence[index + 1].Word_text;
                                        rawSen[i].Word_pos = "Np";
                                        i++;
                                    }

                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                        i++;
                                    }


                                    break;     
                                     * */
                                case "chưa":
                                    if (index == formerSentence.Sentence_length - 2)
                                    {
                                        rawSen[i].Word_pos = "-";
                                    }
                                    else rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                    break;
                                case "rồi":
                                    if (index == formerSentence.Sentence_length - 2)
                                    {
                                        rawSen[i].Word_pos = "T";
                                    }
                                    else rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                    break;
                                    
                                case "bao nhiêu":
                                    rawSen[i].Word_text = "bao nhiêu";
                                    rawSen[i].Word_pos = "Z";
                                    i++;
                                    break;
                                case "đâu":
                                    rawSen[i].Word_text = "đâu";
                                    rawSen[i].Word_pos = "Z";
                                    i++;
                                    break;
                                case "nào":
                                    rawSen[i].Word_text = "nào";
                                    rawSen[i].Word_pos = "Z";
                                    i++;
                                    break;
                                case "ai":
                                    rawSen[i].Word_text = "ai";
                                    rawSen[i].Word_pos = "Z";
                                    i++;
                                    break;
                                case "ấy là":
                                    rawSen[i].Word_text = "ấy";
                                    rawSen[i].Word_pos = "P";
                                    i++;
                                    rawSen[i].Word_text = "là";
                                    rawSen[i].Word_pos = "V";
                                    i++;
                                    break;
                                case "phải":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("có"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                        i++;
                                    }
                                    break;
                                case "hay":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("có"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                        i++;
                                    }
                                    break;
                                case "không":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("có") || rawSen[i - 1].Word_text.ToLower().Contains("đúng") || rawSen[i - 1].Word_text.ToLower().Contains("phải"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                        i++;
                                    }
                                    break;
                                case "là":
                                    string strVerb = "nghĩ,muốn,thích,yêu,được";
                                    if (rawSen[i - 1].Word_pos.Equals("V"))
                                    {
                                        if (strVerb.Contains(rawSen[i - 1].Word_text.ToLower()))
                                        {
                                            rawSen[i].Word_text = formerSentence[index].Word_text;
                                            rawSen[i].Word_pos = formerSentence[index].Word_pos; ;
                                            i++;
                                        }
                                        else
                                        {
                                            rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                            rawSen[i - 1].Word_pos = "V";
                                        }
                                    }
                                    else
                                    {
                                        if (rawSen[i - 1].Word_text.ToLower().Contains("có") || rawSen[i - 1].Word_text.ToLower().Contains("như"))
                                        {
                                            rawSen[i - 1].Word_text = formerSentence[index].Word_text;
                                            rawSen[i - 1].Word_pos = "-";
                                        }
                                        else
                                        {
                                            rawSen[i].Word_text = formerSentence[index].Word_text;
                                            rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                            i++;
                                        }
                                    }
                                    break;
                                /*    
                                case "thế":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("như"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = "-" ;
                                        i++;
                                    }
                                    break;
                                case "vậy":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("như"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = "-";
                                        i++;
                                    }
                                    break;
                                 */
                                case "nhỉ":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("thế") || rawSen[i - 1].Word_text.ToLower().Contains("vậy"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = "-";
                                        i++;
                                    }
                                    break;
                                case "à":
                                    if (rawSen[i - 1].Word_text.ToLower().Contains("thế") || rawSen[i - 1].Word_text.ToLower().Contains("vậy"))
                                    {
                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                        rawSen[i - 1].Word_pos = "-";
                                    }
                                    else
                                    {
                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                        rawSen[i].Word_pos = "-";
                                        i++;
                                    }
                                    break;
                                default:
                                    switch (formerSentence[index].Word_pos)
                                    {
                                        case "V":
                                            strVerb = "nghĩ,muốn,thích,yêu,được";
                                            if (rawSen[i - 1].Word_pos.Equals("V"))
                                            {
                                                if (strVerb.Contains(rawSen[i - 1].Word_text.ToLower()))
                                                {
                                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                                    rawSen[i].Word_pos = formerSentence[index].Word_pos; ;
                                                    i++;
                                                }
                                                else
                                                {
                                                    rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                                    rawSen[i - 1].Word_pos = "V";
                                                }
                                            }
                                            else
                                            {
                                                rawSen[i].Word_text = formerSentence[index].Word_text;
                                                rawSen[i].Word_pos = "V";
                                                i++;
                                            }
                                            break;
                                        case "N":
                                            if (rawSen[i - 1].Word_pos.Equals("N"))
                                            {
                                                
                                                string strCompo = rawSen[i - 1].Word_text + " " + formerSentence[index].Word_text;
                                                if (XMLNounConnection.IsCompositNoun(strCompo, ""))
                                                {
                                                    rawSen[i - 1].Word_text = strCompo;
                                                }
                                                else
                                                {
                                                    //them xe trau
                                                     if ((rawSen[i - 1].Word_text.ToLower() == "xe") && (formerSentence[index].Word_text.ToLower() == "trâu"))
                                                     {
                                                        rawSen[i - 1].Word_text += " " + formerSentence[index].Word_text;
                                                        rawSen[i - 1].Word_pos = "Np";
                                                     }
                                                         ///
                                                     else
                                                     {
                                                        rawSen[i].Word_text = formerSentence[index].Word_text;
                                                        rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                                        i++;
                                                     }
                                                }
                                            }
                                            else
                                            {
                                                
                                                
                                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                                    rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                                
                                                i++;
                                                
                                            }

                                            break;
                                        case "M":
                                            string strTime = "ngày,tháng,năm";
                                            if ((index > 0) && strTime.Contains(rawSen[index - 1].Word_text.ToLower()))
                                            {
                                                if ((index < rawSen.Sentence_length - 1) && (!strTime.Contains(rawSen[index + 1].Word_text.ToLower())))
                                                {
                                                    rawSen[i - 1].Word_text += " " + rawSen[index].Word_text;
                                                    rawSen[i - 1].Word_pos = "Nt";
                                                }
                                                else
                                                {
                                                    rawSen[i].Word_text = formerSentence[index].Word_text;
                                                    rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                                    i++;
                                                }
                                            }
                                            else
                                            {
                                                rawSen[i].Word_text = formerSentence[index].Word_text;
                                                rawSen[i].Word_pos = formerSentence[index].Word_pos;
                                                i++;
                                            }
                                            break;
                                        default:
                                            rawSen[i].Word_text = formerSentence[index].Word_text;
                                            rawSen[i].Word_pos = formerSentence[index].Word_pos; ;
                                            i++;
                                            break;
                                    }
                                    break;
                            }
                        }
                    }
                }
                #endregion;
                #region inti...
                resultSentence = new sentence_tagger(i-1);
                for (int j = 0; j < i-1; j++)
                {
                    resultSentence[j].Word_pos = rawSen[j+1].Word_pos;
                    resultSentence[j].Word_text = rawSen[j+1].Word_text;
                }
 
                
                #endregion
                return resultSentence;
            }
            catch(Exception ex)
            {
                throw ex;
            }
 
        }

        static public WordStruct[] WordArrayFormArray(sentence_tagger sentag)
        {
            try
            {
                if (sentag != null)
                {
                    int j = 0;
                    for (int k = 0; k < sentag.Sentence_length; k++) if (sentag[k] != null) j++;
                    WordStruct[] outWordArray = new WordStruct[sentag.Sentence_length];
                    int i = 0;
                    for (i = 0; i < j;i++ )
                    {
                        outWordArray[i] = new WordStruct();
                        outWordArray[i].Word = sentag[i].Word_text;
                        outWordArray[i].Pos = sentag[i].Word_pos;
                        outWordArray[i].Index = i;
                        #region fix Gender
                        if (outWordArray[i].Pos.Contains("N"))
                        {
                            if (!string.IsNullOrEmpty(nounconn.GetWordType(outWordArray[i], "C:\\tagger\\XMLdatabase\\NounXMLdatabase.xml")))
                            {
                               if (nounconn.GetWordType(outWordArray[i], "C:\\tagger\\XMLdatabase\\NounXMLdatabase.xml").Equals("People"))
                                {
                                    outWordArray[i].WordSubMean = "People";
                                    outWordArray[i].WordSubMean += " " + nounconn.HumanGender(outWordArray[i], "C:\\tagger\\XMLdatabase\\NounXMLdatabase.xml");
                                }
                               else if (nounconn.GetWordType(outWordArray[i], "C:\\tagger\\XMLdatabase\\NounXMLdatabase.xml").Equals("Place"))
                               {
                                   outWordArray[i].WordSubMean += "Place";
                               }
                               else  outWordArray[i].WordSubMean += "Things";
                            }
                            //else outWordArray[i].WordSubMean = "Things";
                        }
                        #endregion

                        #region fixnumberfornoun  // so luong cua danh tu
                        if (i == 0)
                        {
                            if(outWordArray[i].Pos.Equals("P"))
                            {
                                outWordArray[i].WordSubMean += "Sing";
                                if(nounconn.IsNounHuman(outWordArray[i].Word,"")) outWordArray[i].WordSubMean+="People";
                                else outWordArray[0].WordSubMean+="Things";
                            }
                        }
                        if (i > 0)
                        {
                            if (outWordArray[i].Pos.Equals("N") || (outWordArray[i].Pos.Equals("Np")))
                            {
                                
                               
                                    if (!outWordArray[i - 1].Pos.Equals("M")) outWordArray[i].WordSubMean += "Sing";
                                    else
                                        if (!outWordArray[i - 1].Pos.Equals("Nc"))
                                        {
                                            if (!outWordArray[i - 1].Pos.Equals("L"))
                                            {
                                                if ((!outWordArray[i - 1].Pos.Equals("M")) || (!outWordArray[i - 1].Word.Contains("nhiều"))) outWordArray[i].WordSubMean += "Sing";
                                                else
                                                {
                                                    if (outWordArray[i - 1].Word.Equals("một") || (string.IsNullOrEmpty(outWordArray[i - 1].Word))) outWordArray[i].WordSubMean += "Sing";
                                                    else
                                                    {
                                                        outWordArray[i].WordSubMean += "Plu";
                                                    }
                                                }
                                            }
                                            else outWordArray[i].WordSubMean += "Plu";
                                        }
                                        else
                                        {
                                            if (i > 1)
                                            {
                                                if (!outWordArray[i - 2].Pos.Equals("L"))
                                                {
                                                    if (!outWordArray[i - 2].Pos.Equals("M")) outWordArray[i].WordSubMean += "Sing";
                                                    else
                                                    {
                                                        if (outWordArray[i - 2].Word.ToLower().Equals("một") || (string.IsNullOrEmpty(outWordArray[i - 2].Word))) outWordArray[i].WordSubMean += "Sing";
                                                        else
                                                        {
                                                            outWordArray[i].WordSubMean += "Plu";
                                                        }
                                                    }
                                                }
                                                else outWordArray[i].WordSubMean += "Plu";
                                            }
                                            else outWordArray[i].WordSubMean += "Sing";
                                        }
                                                               
                            }
                            //Vinh: P ở giữa câu
                            else if (outWordArray[i].Pos.Equals("P") && (outWordArray[i].Word.ToLower().Contains("anh ấy") || outWordArray[i].Word.ToLower().Contains("anh ta") || outWordArray[i].Word.ToLower().Contains("ông ta")))
                            {
                                outWordArray[i].WordSubMean += "Sing";
                                if (nounconn.IsNounHuman(outWordArray[i].Word, "")) outWordArray[i].WordSubMean += "PeopleXO";
                                else outWordArray[0].WordSubMean += "Things";
                            }
                            else if (outWordArray[i].Pos.Equals("P") && (outWordArray[i].Word.ToLower().Contains("chị ta") || outWordArray[i].Word.ToLower().Contains("cô ta")))
                            {
                                outWordArray[i].WordSubMean += "Sing";
                                if (nounconn.IsNounHuman(outWordArray[i].Word, "")) outWordArray[i].WordSubMean += "PeopleXX";
                                else outWordArray[0].WordSubMean += "Things";
                            }
                            else if (outWordArray[i].Pos.Equals("P") && (outWordArray[i].Word.ToLower().Contains("xe này") || outWordArray[i].Word.ToLower().Contains("xe đó") || outWordArray[i].Word.ToLower().Contains("cái này")|| outWordArray[i].Word.ToLower().Contains("hiện vật này")))
                            {
                                outWordArray[i].WordSubMean += "Sing";
                                if (nounconn.IsNounHuman(outWordArray[i].Word, "")) outWordArray[i].WordSubMean += "PeopleXO";
                                else outWordArray[i].WordSubMean += "Things";
                            }
                        }
                        else if (outWordArray[i].Pos.Equals("N") || outWordArray[i].Pos.Equals("Np")) outWordArray[i].WordSubMean += "Sing";
                        //Vinh: P ở giữa câu
                        
                        #endregion

                        #region fixNandNp  // tim dai tu can thay the va loai tu cua no
                        if (outWordArray[i].Pos.Equals("P") && (!outWordArray[i].Word.ToLower().Contains("anh ấy") && !outWordArray[i].Word.ToLower().Contains("anh ta") && !outWordArray[i].Word.ToLower().Contains("ông ta") && !outWordArray[i].Word.ToLower().Contains("chị ta")&& !outWordArray[i].Word.ToLower().Contains("cô ta")))
                        {
                            if (i > 0)
                            {

                                if ((outWordArray[i - 1].Pos.Equals("N")) || (outWordArray[i - 1].Pos.Equals("Np")))
                                {

                                    outWordArray[i].WordSubMean = outWordArray[i - 1].WordSubMean;
                                    outWordArray[i].Ower = i - 1;
                                }
                                else
                                {
                                    if (outWordArray[i - 1].Pos.Equals("P")) outWordArray[i].Ower = -1;
                                    if (outWordArray[i - 1].Pos.Equals("A"))
                                    {
                                        if ((i > 1) && (outWordArray[i - 2].Pos.Contains("N")))
                                        {
                                            outWordArray[i].WordSubMean = outWordArray[i - 2].WordSubMean;
                                            outWordArray[i].Ower = i - 2;
                                        }
                                        else
                                        {
                                            outWordArray[i].WordSubMean = outWordArray[i].WordSubMean;
                                            outWordArray[i].Ower = i;
                                        }
                                    }
                                    else
                                    {
                                        if (outWordArray[i].Word.ToLower().Equals("nó")) outWordArray[i].WordSubMean += "Things People X? Sing";
                                        else
                                        {
 
                                        }
                                        outWordArray[i].Ower = i;
                                    }
                                }
                            }
                            else
                            {
                                outWordArray[i].Ower = i;
                                
                            }
                            if ((outWordArray[i].Word.Contains("chúng")) || (outWordArray[i].Word.Contains("họ")))
                            {
                                outWordArray[i].WordSubMean += "Plu";
                            }
                        }
                        #endregion

                        #region fixAskWork
                        if(sentag[sentag.Sentence_length-1].Word_text.Equals("?"))
                        {
                            if(sentag[i].Word_pos.Equals("Z"))
                            {
                                switch (sentag[i].Word_text.ToLower())
                                {
                                    case "đâu": 
                                        outWordArray[i].Ower = i;
                                        outWordArray[i].WordSubMean = "Thing Sing Place";
                                        break;
                                    case "ai":
                                        outWordArray[i].Ower = i;
                                        outWordArray[i].WordSubMean = "People Sing X?";
                                        break;
                                    
                                }
                            }
                        }

                        #endregion

                    }
                    return outWordArray;
                }
                else return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}
